package com.fly.video.downloader;

import com.fly.video.downloader.bean.Video;
import com.fly.video.downloader.content.Recv;
import com.fly.video.downloader.core.app.BaseActivity;
import com.fly.video.downloader.core.content.ClipboardManager;
import com.fly.video.downloader.layout.fragment.HistoryFragment;
import com.fly.video.downloader.layout.fragment.VideoFragment;
import com.fly.video.downloader.layout.fragment.VideoSearchFragment;
import com.fly.video.downloader.slice.MainAbilitySlice;
import com.fly.video.downloader.util.Helpers;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;
import ohos.app.Environment;
import ohos.app.dispatcher.task.TaskPriority;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class MainAbility extends BaseActivity {
    private static final String TAG = MainAbility.class.getSimpleName();
    protected ProgressBar progressBar;
    protected VideoFragment videoFragment;
    protected HistoryFragment historyFragment;
    protected VideoSearchFragment searchFragment = null;
    private boolean fromSend = false;
    private Button mViewbtn,mHitoryBtn,mSearchBtn;

    public Component.ClickedListener mHistoryClick=new Component.ClickedListener(){

        @Override
        public void onClick(Component component) {
//            getFractionManager().startFractionScheduler().replace(ResourceTable.Id_full_pager,historyFragment).submit();
            getFractionManager().startFractionScheduler().hide(videoFragment).hide(searchFragment).show(historyFragment).submit();

        }
    };
    public Component.ClickedListener mViewClick=new Component.ClickedListener(){

        @Override
        public void onClick(Component component) {
//            getFractionManager().startFractionScheduler().replace(ResourceTable.Id_full_pager,videoFragment).submit();
            getFractionManager().startFractionScheduler().hide(historyFragment).hide(searchFragment).show(videoFragment).submit();
        }
    };
    public Component.ClickedListener mSearchClick=new Component.ClickedListener(){

        @Override
        public void onClick(Component component) {
            showVideoSearchFragment();
//            getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(new Runnable() {
//                @Override
//                public void run() {
//
//                    testDownLoad();
//                }
//            });
        }
    };
    private String lastClip = "";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(MainAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_main_new);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        progressBar= (ProgressBar) findComponentById(ResourceTable.Id_progress);
        mViewbtn= (Button) findComponentById(ResourceTable.Id_navigation_video);
        mViewbtn.setClickedListener(mViewClick);
        mHitoryBtn= (Button) findComponentById(ResourceTable.Id_navigation_history);
        mHitoryBtn.setClickedListener(mHistoryClick);
        mSearchBtn= (Button) findComponentById(ResourceTable.Id_navigation_input);
        mSearchBtn.setClickedListener(mSearchClick);

        videoFragment = VideoFragment.newInstance(this);
        historyFragment = HistoryFragment.newInstance(this);
        searchFragment = VideoSearchFragment.newInstance(this);

        getFractionManager().startFractionScheduler().add(ResourceTable.Id_no_navigation_pager,searchFragment).add(ResourceTable.Id_full_pager, videoFragment).add(ResourceTable.Id_view_pager, historyFragment).hide(historyFragment).hide(searchFragment).show(videoFragment).submit();
//        getFractionManager().startFractionScheduler().add(ResourceTable.Id_full_pager, videoFragment).show(videoFragment).submit();
        fromSend = this.getIntent() != null && "android.intent.action.SEND".equals(this.getIntent().getAction());

        String[] per = {"ohos.permission.READ_USER_STORAGE", "ohos.permission.WRITE_MEDIA",
                "ohos.permission.READ_MEDIA", "ohos.permission.WRITE_USER_STORAGE"};
        requestPermissionsFromUser(per, 0);
    }

    @Override
    protected void onActive() {
        super.onActive();
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                ClipboardManager clip = new ClipboardManager(getContext());
                String str = clip.getText(0);

                if (!lastClip.equals(str))
                {
                    if (Helpers.containsDouyin(getContext(), str)) {
                        lastClip = str;
                        showVideoSearchFragment();
                        searchFragment.setText(str);
                        new ToastDialog(getContext()).setContentText(getString(ResourceTable.String_clip_valid)).show();
                    }
                }
            }
        });
    }
    public void showVideoSearchFragment()
    {
        getFractionManager().startFractionScheduler().hide(historyFragment).hide(videoFragment).show(searchFragment).submit();
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        fromSend = false;
        if(videoFragment!=null){
            getFractionManager().startFractionScheduler().hide(historyFragment).hide(searchFragment).show(videoFragment).submit();
        }
        Recv recv = new Recv(intent);
        if (recv.isActionSend()
//                && videoFragment.isAdded()
        ) {
            fromSend = true;
            if(videoFragment!=null){
                videoFragment.Analyze(recv.getContent());
            }
        }
    }
    public void setMainProgress(int progress)
    {
        if (progress >= 100)
            progress = 0;
        int finalProgress = progress;
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                progressBar.setProgressValue(finalProgress);
            }
        });
    }
    public void onVideoChange(String str)
    {
        //Toast.makeText(this, str, Toast.LENGTH_LONG).show();
//        getFractionManager().startFractionScheduler().replace(ResourceTable.Id_full_pager,videoFragment).submit();
        getFractionManager().startFractionScheduler().hide(historyFragment).hide(searchFragment).show(videoFragment).submit();
        videoFragment.Analyze(str);
    }

    public void onVideoChange(Video video)
    {
        onVideoChange(video, false);
    }

    public void onVideoChange(Video video, boolean fromHistory)
    {
        getFractionManager().startFractionScheduler().hide(historyFragment).hide(searchFragment).show(videoFragment).submit();
        videoFragment.Analyze(video, fromHistory);
    }

    public void onHistoryAppend(Video video)
    {
        historyFragment.perpendHistory(video);
    }
}
